package com.gexiaobao.pigeon.app.util

import android.graphics.Color
import androidx.core.content.ContextCompat
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.PigeonRaceCharResponse
import com.gexiaobao.pigeon.ui.view.MyMarkerView
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.ValueFormatter
import me.hgj.jetpackmvvm.base.appContext
import java.text.DecimalFormat


/**
 * @Author : hxw
 * @Date : 2024/3/23 14:03
 * @Describe :
 */
class LineChartManager(private val lineChart: LineChart) {

    private val leftAxis: YAxis = lineChart.axisLeft
    private val rightAxis: YAxis = lineChart.axisRight
    private val xAxis: XAxis = lineChart.xAxis

    private lateinit var lineData: LineData

    fun setUpLineChart(xAxisData2: MutableList<String>, it: PigeonRaceCharResponse, maxValue: Float) {
        xAxisData2.clear()
        val lineEntries: MutableList<Entry> = ArrayList()
        val lineEntries2: MutableList<Entry> = ArrayList()
        if (it.me != null && it.me!!.chartGrades.isNotEmpty()) {
            it.me!!.chartGrades.forEachIndexed { i, chartGradesBean ->
                lineEntries.add(Entry(i.toFloat(), chartGradesBean.speed))
                xAxisData2.add(chartGradesBean.disStr)
            }
        }

        if (it.other != null && it.other!!.chartGrades.isNotEmpty()) {
            it.other!!.chartGrades.forEachIndexed { i, chartGradesBean ->
                lineEntries2.add(Entry(i.toFloat(), chartGradesBean.speed))
            }
        }

        val lineDataSet = LineDataSet(lineEntries, "我的")
        val lineDataSet2 = LineDataSet(lineEntries2, "冠军")
        setLine1(lineDataSet, ContextCompat.getColor(appContext, R.color.colorPrimaryDark))
        setLine2(lineDataSet2, ContextCompat.getColor(appContext, R.color.color_fe4542))
        setLegend()

        lineDataSet2.color = ContextCompat.getColor(appContext, R.color.color_fe4542)
        lineData = LineData(lineDataSet, lineDataSet2)
        lineChart.data = lineData
        //绘制网格线
        xAxis.enableGridDashedLine(8f, 6f, 0f)
        // 设置X轴顶部的偏移量为10%
//        xAxis.spaceMax = 0.1f
        leftAxis.enableGridDashedLine(8f, 6f, 0f)
        rightAxis.enableGridDashedLine(8f, 6f, 0f)

        //设置一页最大显示个数为7，超出部分就滑动
        val ratio = xAxisData2.size.toFloat() / 7f
        //显示的时候是按照多大的比率缩放显示,1f表示不放大缩小
        lineChart.zoom(ratio, 1f, 0f, 0f)

        lineChart.animateX(1000)
        lineChart.xAxis.isEnabled = true
        //设置Y轴最大值
        leftAxis.axisMaximum = maxValue
        rightAxis.axisMaximum = maxValue
//        leftAxis.resetAxisMaximum()
//        rightAxis.resetAxisMaximum()
        //关闭边框矩形
        lineChart.setDrawBorders(false)
        //隐藏图表右下角显示内容
        val description = Description()
        description.isEnabled = false
        lineChart.description = description
        //设置图例到X轴的距离
        lineChart.extraBottomOffset = 20f
        if (xAxisData2.isNotEmpty()) {
            try {
                xAxis.valueFormatter = object : ValueFormatter() {
                    override fun getFormattedValue(value: Float): String {
                        return if (value.toInt() != -1 && xAxisData2.size != 0) {
                            xAxisData2[value.toInt() % xAxisData2.size]
                        } else {
                            ""
                        }
                    }
                }
            } catch (e: ArithmeticException) {
                e.printStackTrace()
            }
        }
        // 这里的5表示Y轴最多显示5个刻度值
        leftAxis.labelCount = 6
        rightAxis.labelCount = 6
        lineChart.marker = MyMarkerView(appContext, xAxisData2)
        //刷新
        lineChart.invalidate()
    }

    private fun setLine2(set: LineDataSet, color: Int) {
        //虚线模式下绘制直线
//        set.enableDashedLine(20f, 5f, 0f)
        //点击后高亮线的显示颜色
        set.enableDashedHighlightLine(50f, 15f, 0f)
        //设置数据小圆点的颜色
        set.setCircleColor(ContextCompat.getColor(appContext, R.color.color_fe4542))
        //设置圆点是否有空心
        set.setDrawCircles(true)
        //设置线条的宽度，最大10f,最小0.2f
        set.lineWidth = 2f
        //设置小圆点的半径，最小1f，默认4f
        set.circleRadius = 5f
        //设置是否显示小圆点
        set.setDrawCircles(true)
        //设置字体颜色
        set.valueTextColor = ContextCompat.getColor(appContext, R.color.color_fe4542)
        //设置字体大小
        set.valueTextSize = 16f
        //设置是否填充
        set.setDrawFilled(false)
        set.color = color
        val decimalFormat = DecimalFormat("0.0000")
        set.valueFormatter = object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
                return decimalFormat.format(value) // 使用DecimalFormat格式化值
            }
        }
    }

    private fun setLine1(set: LineDataSet, color: Int) {
        //虚线模式下绘制直线
//        set.enableDashedLine(20f, 5f, 0f)
        //点击后高亮线的显示颜色
        set.enableDashedHighlightLine(50f, 15f, 0f)
        //设置数据小圆点的颜色
        set.setCircleColor(color)
        //设置圆点是否有空心
        set.setDrawCircles(true)
        //设置线条的宽度，最大10f,最小0.2f
        set.lineWidth = 2f
        //设置小圆点的半径，最小1f，默认4f
        set.circleRadius = 5f
        //设置是否显示小圆点
        set.setDrawCircles(true)
        //设置字体颜色
        set.valueTextColor = color
        //设置字体大小
        set.valueTextSize = 16f
        //设置是否填充
        set.setDrawFilled(false)
        set.color = color
        val decimalFormat = DecimalFormat("0.0000")
        set.valueFormatter = object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
                return decimalFormat.format(value) // 使用DecimalFormat格式化值
            }
        }
    }

    /**
     * 初始化LineChart
     */
    fun initLineChart() {
        xAxis.yOffset = 10f
        leftAxis.xOffset = 30f
        rightAxis.xOffset = 30f
        //设置X轴的字体大小
        xAxis.textSize = 15f
        lineChart.setDrawGridBackground(false)
        //设置Y轴的字体大小
        leftAxis.textSize = 15f
        rightAxis.textSize = 15f
        rightAxis.isEnabled = true
        //设置X轴Y轴的线颜色
        xAxis.axisLineColor = Color.TRANSPARENT
        leftAxis.axisLineColor = Color.TRANSPARENT
        rightAxis.axisLineColor = Color.TRANSPARENT
        //不绘制X轴网格线
        xAxis.setDrawGridLines(false)
        leftAxis.gridLineWidth = 1.2f
        rightAxis.gridLineWidth = 1.2f
        //显示边界
        lineChart.setDrawBorders(true)
        //设置是否可以拖拽
        lineChart.isDragEnabled = true
        //设置一页最大显示个数为8，超出部分就滑动
//        val ratio = 11f / 10f
        //XY轴的设置
        //X轴设置显示位置在底部
        xAxis.position = XAxis.XAxisPosition.BOTTOM
        xAxis.axisMinimum = 0f
        xAxis.granularity = 1f
        //保证Y轴从0开始，不然会上移一点
        leftAxis.axisMinimum = 0f
        rightAxis.axisMinimum = 0f
        lineChart.invalidate()
    }

    /**
     * 设置图例
     */
    private fun setLegend() {
        val legend: Legend = lineChart.legend
        legend.textColor = ContextCompat.getColor(appContext, R.color.comm_text_gray_dark)
        legend.textSize = 15f
        //设置图例垂直对齐
        legend.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
        //设置图例居中
        legend.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
        //设置图例方向
        legend.orientation = Legend.LegendOrientation.HORIZONTAL
        //设置图例是在图内还是图外绘制
        legend.setDrawInside(false)
        //图例条目之间距离
        legend.xEntrySpace = 14f
        //设置图例可否换行
        legend.isWordWrapEnabled = true
        //设置图例现状为线.默认为方形
        legend.form = Legend.LegendForm.LINE
        //是否隐藏图例/true_否，false_是
        legend.isEnabled = true
    }

}
